﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using StockCommon;
using StockImport.http;
using System.IO;
using Utilities;
using StockImport.TaskLog;

namespace StockImport.dataimport.otc.dailyindex
{
    public class OTC_DailyIndexFetcher : HttpContentFetcher_Daily
    {
        //這個連結~~之前沒用~~
        DateTime valieStartDate = DateTime.Parse("2012/05/29");
        DateTime valieEndDate = DateTime.Parse("2013/05/29");

        
        public override bool suitableForDate(DateTime d)
        {
            if ((valieStartDate.CompareTo(d) <= 0) &&
              (valieEndDate.CompareTo(d) >= 0))
                return true;
            return false;
        }

        public static String getURL(DateTime date)
        {
            String pat = "http://www.gretai.org.tw/ch/stock/aftertrading/otc_quotes_no1430/squote_EW_${yy}${MM}${dd}.html";
            return replaceYMD(pat, date);
        }
        /**********************************************************************************
         * 
         * 
         * *********************************************************************************/

        public override String getVersion()
        {
            return "2.0";
        }
        public override String getTaskName()
        {
            return "上櫃_股票行情";
        }

        public override UpdateResult update(DateTime date, bool force = false)
        {
            UpdateResult result = new UpdateResult();
            if (suitableForDate(date) == false)
            {
                result.setMessage("本 fetcher 不適用該日期");
                return result;
            }

            if (force == false)
            {
                if (date.DayOfWeek.Equals(DayOfWeek.Sunday) ||
                    date.DayOfWeek.Equals(DayOfWeek.Saturday))
                {
                    result.setUpdateResult(true);
                    result.setMessage("假日不需更新");
                    return result;
                }
            }

            String fileName = getStoreFileName(date);
            if (System.IO.File.Exists(fileName))
            {
                if (force == false)
                {
                    result.setUpdateResult(true);
                    result.setContentResult(true);
                    result.setMessage("檔案已存在不需更新");
                    return result;                
                }
                System.IO.File.Delete(fileName);
            }
            String url = getURL(date);


            result=UrlGet.process(url);

            return checkResult(result, date);

        }

        private UpdateResult checkResult(UpdateResult result, DateTime date)
        {
            String fileName = getStoreFileName(date);
            String content = result.getContent();
            if (result.getUrlResult())
            {
                if (result.hasContent())
                {
                    if (contentCheck(content))
                    {
                        try//寫入
                        {
                            File.WriteAllText(fileName, content,Encoding.GetEncoding("utf-8"));
                            result.setContentResult(true);
                            result.setUpdateResult(true);
                            result.setStoreFileName(fileName);
                            result.setMessage("update:\t" + fileName);
                            return result;
                        }
                        catch (Exception e)
                        {
                            result.setMessage(e.Message);
                        }
                    }
                    else
                    {
                        result.setUpdateResult(true);
                        result.setMessage("資料無效或不存在");
                    }
                }
                else
                {
                    result.setUpdateResult(true);
                    result.setMessage("資料無效或不存在");
                }
            }
            else
            {
                result.setMessage("Http無法取得資料");
            }
            return result;
        }

        private bool contentCheck(string content)
        {
            if (String.IsNullOrWhiteSpace(content)==false)
            {
                if (content.IndexOf("您所輸入的網址並不存在") <0)
                    return true;
            }
            return false;
        }
        private static String getStorePath()
        {
            return @"D:\stockdata\otc\上櫃_股票行情\";
        }
        public static string getStoreFileName(DateTime date)
        {
            String sp=getStorePath();
            sp += String.Format("original\\{0}.html", DateUtils.getYYYYMMDD_NoSlash(date));
            return sp;
        }

    }
}
